/*
 *
 * File Name: at_dbus.h
 * Author: liaoxuecheng
 * mail: liaoxuecheng@hotmail.com
 * Created Time: 2024年12月26日 星期四 10时42分10秒
 *
 */

#ifndef _AT_DBUS_H_
#define _AT_DBUS_H_

#include <dbus/dbus.h>

#define VARIANT_MAX_ARGS    128

typedef struct _at_variant{
    int n_args;
    char type[VARIANT_MAX_ARGS]; //not string
    void *args[VARIANT_MAX_ARGS]; 
}at_variant;

typedef struct _at_proxy{
    char *destination;
    char *object_path;
    char *interface;
}at_proxy;

void at_dbus_init_common_connection(pthread_mutex_t *mutex, long max_len);
void at_variant_free(at_variant *variant);
at_variant *at_variant_new(const char *format_string, ...);
at_variant *at_variant_copy(at_variant *value);
dbus_bool_t at_variant_get(const at_variant *value,const char *format_string, ...);
at_variant *get_variant_from_message(DBusMessage *message);

void at_proxy_free(at_proxy *proxy);
at_proxy *at_proxy_new(char *destination, char *object_path, char *interface);

at_variant *at_dbus_call_sync(DBusConnection *conn,
        at_proxy *proxy,
        const char *method_name,
        at_variant *parameters,
        DBusError *error);

void at_dbus_call_async(DBusConnection *conn,
        at_proxy *proxy,
        const char *method_name,
        at_variant *parameters,
        DBusPendingCallNotifyFunction callback,
        void *user_data);

void at_dbus_call_async2(DBusConnection *conn,
        at_proxy *proxy,
        const char *method_name,
        at_variant *parameters,
        DBusPendingCallNotifyFunction callback,
        void *user_data,
        DBusFreeFunction free_user_data);

void at_dbus_call(DBusConnection *conn,
        at_proxy *proxy,
        const char *method_name,
        at_variant *parameters);

void at_dbus_add_signal_match(DBusConnection* conn, const char *destination);
void at_dbus_remove_signal_match(DBusConnection* conn, const char *destination);

dbus_bool_t at_dbus_send_signal(DBusConnection *conn,
        const char *obj_path,
        const char *interface,
        const char *signal_name,
        at_variant *parameters);

dbus_bool_t at_dbus_send_message_response_success(DBusConnection *conn,
        DBusMessage *message,
        at_variant *reply);
dbus_bool_t at_dbus_send_message_response_error(DBusConnection *conn,
        DBusMessage *message, 
        const char *error,                      
        const char *text);

#endif //_AT_DBUS_H_
